package com.bookmall.member.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bookmall.common.entity.to.MemberInfoTo;
import com.bookmall.common.utils.MyQuery;
import com.bookmall.common.utils.PageUtils;
import com.bookmall.member.domain.MemberInfo;
import com.bookmall.member.domain.MemberLikeBooks;
import com.bookmall.member.domain.vo.BookInfoVo;
import com.bookmall.member.feign.ProductFeignService;
import com.bookmall.member.interceptor.MemberInterceptor;
import com.bookmall.member.mapper.MemberInfoMapper;
import com.bookmall.member.mapper.MemberLikeBooksMapper;
import com.bookmall.member.service.IMemberLikeBooksService;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.web.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author 胡菜鸡
 * @Create 2022-04-01-10:52
 */
@Service
public class MemberLikeBooksServiceImpl extends ServiceImpl<MemberLikeBooksMapper, MemberLikeBooks> implements IMemberLikeBooksService {
    @Autowired
    private ProductFeignService productFeignService;

    @Autowired
    private MemberInfoMapper memberInfoMapper;

    /**
     * 获取会员喜欢列表
     * @author 胡菜鸡
     * @date 2022/5/7 15:30
     */
    @Override
    public PageUtils getMemberLikesWithMemberInfo(MemberLikeBooks memberLikeBooks) {
        IPage<MemberLikeBooks> page = page(new MyQuery<MemberLikeBooks>().getPage(memberLikeBooks), new LambdaQueryWrapper<>(memberLikeBooks));
        List<MemberLikeBooks> collect = page.getRecords().stream().map(memberLikeBook -> {
            MemberInfo memberInfo = memberInfoMapper.selectById(memberLikeBook.getMemberId());
            memberLikeBook.setMemberName(memberInfo.getNickname());
            LambdaQueryWrapper<MemberInfo> wrapper = new LambdaQueryWrapper<>();
            MemberInfo publishMemberInfo = memberInfoMapper.selectOne(wrapper.eq(MemberInfo::getStuId, memberLikeBook.getPublishMemberId()));
            memberLikeBook.setPublishMemberName(publishMemberInfo.getNickname());
            return memberLikeBook;
        }).collect(Collectors.toList());
        page.setRecords(collect);
        return new PageUtils(page);
    }

    /**
     * 新增会员喜欢书籍
     * @author 胡菜鸡
     * @date 2022/5/7 16:16
     */
    @Override
    public int saveMemberLikesBook(MemberLikeBooks memberLikeBooks) throws ServiceException {
        String bookId = memberLikeBooks.getBookId();
        String memberId = memberLikeBooks.getMemberId();
        LambdaQueryWrapper<MemberLikeBooks> wrapper = new LambdaQueryWrapper<>();
        Long count = baseMapper.selectCount(wrapper.eq(MemberLikeBooks::getMemberId, memberId).eq(MemberLikeBooks::getBookId, bookId));
        if(count > 0){
            throw new ServiceException("该书籍已存在于您的喜欢列表中");
        }
        return save(memberLikeBooks) ? 1 : 0;
    }
}
